package com.example.springbootlocal.service.Impl;

import com.example.springbootlocal.domain.Student;
import com.example.springbootlocal.service.StudentService;
import com.example.springbootlocal.utils.ListUtils;
import com.example.springbootlocal.utils.ObjectUtils;
import com.example.springbootlocal.utils.Strings;
import com.example.springbootlocal.utils.page.IPage;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author X.I.O
 * @title: SyudentServiceImpl
 * @projectName spring-boot-local
 * @description: TODO
 * @date 2021/11/9 16:18
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Override
    public void saveStudent(Student student) {
        ObjectUtils.saveObj(student,Student.class);
    }

    @Override
    public void saveStudentList(List<Student> students) {
        ObjectUtils.coverList(students,Student.class);
    }

    @Override
    public List<Student> getStudentListByKeyWord(String keyword) {
        List<Student> students=ObjectUtils.getList(Student.class);
        return students.stream().filter(
                e->{
                    if(keyword==null || keyword.length()==0 ){
                        return true;
                    }
                    return Strings.getString(e.getDesc()).contains(keyword) ||
                            Strings.getString(e.getName()).contains(keyword) ||
                            Strings.getString(e.getAge()).contains(keyword);
                }
        ).collect(Collectors.toList());
    }

    @Override
    public IPage<Student> getIPageController(Integer page, Integer size) {
        List<Student> students=ObjectUtils.getList(Student.class);
        IPage<Student> studentIPage=ListUtils.pageListDeal(students,page,size);
        return studentIPage;
    }

    @Override
    public void autoNumSaveStudent(Student student) {
        ObjectUtils.autoNumSaveObjById(student);
    }

    @Override
    public void deleteById(Integer id) {
        ObjectUtils.deleteById(Student.class,id);
    }

    @Override
    public void updateById(Student student) {
        ObjectUtils.updateById(student);
    }
}
